import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet(name = "stu", value = "/stu")
public class stu extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从数据库取出数据,封装到一个集合
        ArrayList<Student> list = new ArrayList<>();
        String sql="select * from student";
        ResultSet rs = DBUtil.query(sql);
        try {
            while (rs.next()){
                int id = rs.getInt("id");
                int age = rs.getInt("age");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                String address = rs.getString("address");
                // 将查到的每行数据,封装成一个学生对象,再把学生对象,封装到集合
                Student stu = new Student(id, age, name, sex, address);
                list.add(stu);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(list);
        // 将集合请求转发给/stu.jsp
        request.setAttribute("stuList",list);// 转发之前.先把集合放入请求域中
        request.getRequestDispatcher("/stu.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
